package com.zb.mail;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

//带附件的邮件
public class EnclosureMail {

    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.setProperty("mail.host", "smtp.qq.com");
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("594507903@qq.com", "wtacdribubspbdhh");
            }
        });
        session.setDebug(true);

        Transport transport = session.getTransport();
        transport.connect();

        //===============================只有信息内容与简单邮件不同================================//
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("594507903@qq.com"));
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("594507903@qq.com"));
        message.setSubject("带附件的邮件");

        MimeBodyPart body1 = new MimeBodyPart();
        DataHandler body1Handler = new DataHandler(new FileDataSource("/Users/juzi/Documents/idea-Workspace/MyResource/java-web-servlet/mail-java/邮件原理.png"));
        body1.setDataHandler(body1Handler);
        body1.setContentID("1.png");

        MimeBodyPart body2 = new MimeBodyPart();
        body2.setContent("这是带图片<img src='cid:1.png' />的邮件", "text/html;charset=utf-8");

        MimeBodyPart body3 = new MimeBodyPart();
        DataHandler body3handler = new DataHandler(new FileDataSource("/Users/juzi/Documents/idea-Workspace/MyResource/java-web-servlet/mail-java/pom.xml"));
        body3.setDataHandler(body3handler);
        body3.setFileName("pom.xml");

        MimeBodyPart body4 = new MimeBodyPart();
        body4.setDataHandler(new DataHandler(new FileDataSource("/Users/juzi/Documents/idea-Workspace/MyResource/java-web-servlet/mail-java/test1.txt")));
        body4.setFileName("");

        //拼接邮件正文内容
        MimeMultipart bodyMultipart = new MimeMultipart();
        bodyMultipart.addBodyPart(body1);
        bodyMultipart.addBodyPart(body2);
        bodyMultipart.setSubType("related");

        //将拼接好的正文内容设置为主体
        MimeBodyPart relatedBody = new MimeBodyPart();
        relatedBody.setContent(bodyMultipart);

        //
        MimeMultipart allMultipart = new MimeMultipart();
        allMultipart.addBodyPart(body3);
        allMultipart.addBodyPart(body4);
        allMultipart.addBodyPart(relatedBody);
        allMultipart.setSubType("mixed");

        message.setContent(allMultipart);
        message.saveChanges();
        //===============================只有信息内容与简单邮件不同================================//

        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
